package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.If;

import com.google.gson.Gson;
import com.ysd.Dao.Impl.BannerDaoImpl;
import com.ysd.Dao.Impl.ProductDaoImpl;
import com.ysd.entity.Banner;
import com.ysd.entity.Product;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminProductServlet extends BaseServlet {


/**
 * 带条件分页查询产品
 * @author 86176
 *地址localhost:8080/zhubao/admin/adminProduct?reqName=getProducts
 */
	public void getProducts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		int productId =0;
		if(request.getParameter("productId")!=null&&!request.getParameter("productId").equals("")) {
			productId =Integer.parseInt(request.getParameter("productId"));
		}
		String productName = request.getParameter("productName");
		int productPrice=0;
		if(request.getParameter("productPrice") !=null && !request.getParameter("productPrice").equals("")) {//productPrice 市场价
			productPrice =Integer.parseInt(request.getParameter("productPrice"));
		}
		int beginPrice=0;
		if(request.getParameter("beginPrice") !=null && !request.getParameter("beginPrice").equals("")) {
			beginPrice =Integer.parseInt(request.getParameter("beginPrice"));
		}
		int endPrice=0;
		if(request.getParameter("endPrice")!=null&&!request.getParameter("endPrice").equals("")) {
			endPrice =Integer.parseInt(request.getParameter("endPrice"));
		}		
		String productTexture = request.getParameter("productTexture");
		int productCategoryId=0;
		if(request.getParameter("productCategoryId")!=null&&!request.getParameter("productCategoryId").equals("")) {
			productCategoryId =Integer.parseInt(request.getParameter("productCategoryId"));
		}
		String productPicture = request.getParameter("productPicture");
		String productMaterial = request.getParameter("productMaterial");
		String productBrand = request.getParameter("productBrand");
		String productSize = request.getParameter("productSize");
		String productNetDegree = request.getParameter("productNetDegree");
		String productColor = request.getParameter("productColor");
		String isCutomize = request.getParameter("isCutomize");
		String productSpecification = request.getParameter("productSpecification");
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));

		Product p = new Product(productId, productName, productPrice, 0, beginPrice, endPrice, productTexture,
				productCategoryId, productPicture, productMaterial, productBrand, productSize, productNetDegree,
				productColor, isCutomize, productSpecification, "");
		ProductDaoImpl pDao = new ProductDaoImpl();
		List<Product> product = pDao.getProductsByWhere(p,(page-1)*size,size);
		Gson gson = new Gson();
		String res = gson.toJson(product);
		out.print(res);

		out.flush();
		out.close();

	}
	/**
	 * 获取查询的集合长度
	 * localhost:8080/zhubao/admin/adminProduct?reqName=getAllProducts
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getAllProducts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		int productId =0;
		if(request.getParameter("productId")!=null&&!request.getParameter("productId").equals("")) {
			productId =Integer.parseInt(request.getParameter("productId"));
		}
		String productName = request.getParameter("productName");
		int productPrice=0;
		if(request.getParameter("productPrice") !=null && !request.getParameter("productPrice").equals("")) {//productPrice 市场价
			productPrice =Integer.parseInt(request.getParameter("productPrice"));
		}
		int beginPrice=0;
		if(request.getParameter("beginPrice") !=null && !request.getParameter("beginPrice").equals("")) {
			beginPrice =Integer.parseInt(request.getParameter("beginPrice"));
		}
		System.out.println(beginPrice);
		int endPrice=0;
		if(request.getParameter("endPrice")!=null&&!request.getParameter("endPrice").equals("")) {
			endPrice =Integer.parseInt(request.getParameter("endPrice"));
		}		
		System.out.println(endPrice);
		String productTexture = request.getParameter("productTexture");
		int productCategoryId=0;
		if(request.getParameter("productCategoryId")!=null&&!request.getParameter("productCategoryId").equals("")) {
			productCategoryId =Integer.parseInt(request.getParameter("productCategoryId"));
		}
		String productPicture = request.getParameter("productPicture");
		String productMaterial = request.getParameter("productMaterial");
		String productBrand = request.getParameter("productBrand");
		String productSize = request.getParameter("productSize");
		String productNetDegree = request.getParameter("productNetDegree");
		String productColor = request.getParameter("productColor");
		String isCutomize = request.getParameter("isCutomize");
		String productSpecification = request.getParameter("productSpecification");

		Product p = new Product(productId, productName, productPrice, 0, beginPrice, endPrice, productTexture,
				productCategoryId, productPicture, productMaterial, productBrand, productSize, productNetDegree,
				productColor, isCutomize, productSpecification, "");
		ProductDaoImpl pDao = new ProductDaoImpl();
		List<Product> product = pDao.getProductsAll(p);


		Gson gson = new Gson();
		String res = gson.toJson(product.size());
		out.print(res);

		out.flush();
		out.close();

	}

	/**
	 * 删除指定产品 localhost:8080/zhubao/adminProduct?reqName=deleteProduct
	 * 
	 * @author 86176
	 *
	 */

	public void deleteProduct(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pId = Integer.parseInt(request.getParameter("productId"));
		ProductDaoImpl pDao = new ProductDaoImpl();
		int n = pDao.deleteProductById(pId);

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		if (n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));

		}

		out.flush();
		out.close();
	}
	
	/**
	 * 批量删除指定产品 localhost:8080/zhubao/adminProduct?reqName=deleteProductByIds
	 * 
	 * @author 86176
	 *
	 */

	public void deleteProductByIds(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pIds = request.getParameter("productId");
		ProductDaoImpl pDao = new ProductDaoImpl();
		int n = pDao.deleteProductByIds(pIds);

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		if (n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));

		}

		out.flush();
		out.close();
	}

	/**
	 * 添加指定产品 
	 * localhost:8080/zhubao/adminProduct?reqName=addProduct
	 * 
	 * @author 86176
	 *
	 */
	public void addProduct(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		ProductDaoImpl pDao = new ProductDaoImpl();

		String productName = request.getParameter("productName");
		String productPicture = request.getParameter("picName");
		int productPrice = Integer.parseInt(request.getParameter("productPrice"));
		int salePrice = Integer.parseInt(request.getParameter("salePrice"));
		String productTexture = request.getParameter("productTexture");
		int productCategoryId = Integer.parseInt(request.getParameter("productCategoryId"));
		String productBrand = request.getParameter("productBrand");
		String productSize = request.getParameter("productSize");
		String productColor = request.getParameter("productColor");
		String productNetDegree = request.getParameter("productNetDegree");
		String productSpecification = request.getParameter("productSpecification");
		String isCutomize = request.getParameter("isCutomize");
		String productMaterial = request.getParameter("productMaterial");

		
		  
		 	Product pcList = pDao.getProductListByName(productName);
			if (pcList == null) {
				Product p = new Product(null, productName, productPrice, salePrice, productTexture, productCategoryId,
						productPicture, productMaterial, productBrand, productSize, productNetDegree, productColor,
						isCutomize, productSpecification);
				int n = pDao.addProduct(p);

				if (n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {
				out.print(Result.toClient(false, "添加失败,产品名重复", ""));
			}
		  	

		out.flush();
		out.close();
	}
	/**
	 * 根据产品Id查询数据,为了数据回显
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProductById(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		int pId = Integer.parseInt(request.getParameter("pId"));
		ProductDaoImpl pDao = new ProductDaoImpl();
		Product pList = pDao.getProductListById(pId);
		
		Gson gson = new Gson();
		String res = gson.toJson(pList);
		out.print(res);
		System.out.println(res);

		out.flush();
		out.close();

	}
	/**
	 * 编辑指定产品 
	 * localhost:8080/zhubao/adminProduct?reqName=editProduct
	 * 
	 * @author 86176
	 *
	 */
	public void editProduct(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		ProductDaoImpl pDao = new ProductDaoImpl();
		
		int productId = Integer.parseInt(request.getParameter("productId"));
		String productName = request.getParameter("productName");
		String productPicture = request.getParameter("picName");
		int productPrice = Integer.parseInt(request.getParameter("productPrice"));
		int salePrice = Integer.parseInt(request.getParameter("salePrice"));
		String productTexture = request.getParameter("productTexture");
		int productCategoryId = Integer.parseInt(request.getParameter("productCategoryId"));
		String productBrand = request.getParameter("productBrand");
		String productSize = request.getParameter("productSize");
		String productColor = request.getParameter("productColor");
		String productNetDegree = request.getParameter("productNetDegree");
		String productSpecification = request.getParameter("productSpecification");
		String isCutomize = request.getParameter("isCutomize");
		String productMaterial = request.getParameter("productMaterial");

		
				Product p = new Product(productId, productName, productPrice, salePrice, productTexture, productCategoryId,
						productPicture, productMaterial, productBrand, productSize, productNetDegree, productColor,
						isCutomize, productSpecification);
				int n = pDao.editProduct(p);

				if (n > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
		

		out.flush();
		out.close();
	}

}
